<template>
	<view class="">
		<view class="content">
			<view class="search-box">
				<!-- mSearch组件 如果使用原样式，删除组件元素-->
				<mSearch class="mSearch-input-box" :mode="2" button="inside" :placeholder="defaultKeyword"
					@search="doSearch(false)" @input_del="input_del" @input="inputChange" @confirm="doSearch(false)"
					v-model="keyword" :keyword="keyword">
				</mSearch>
 

			</view>
			<view class="search-keyword">
				<scroll-view class="keyword-box" scroll-y>
					<view class="" v-if="isinit">
						<view class="keyword-block" v-if="oldKeywordList.length>0">
							<view class="keyword-list-header">
								<view>历史搜索</view>
							</view>
							<view class="keyword">
								<view v-for="(keyword,index) in oldKeywordList" @tap="doSearch(keyword)" :key="index">
									{{keyword}}
								</view>
							</view>
						</view>
					</view>
					<view class="keyword-block" v-if="isinit">
						<view class="keyword-list-header">
							<view>热门搜索</view>

						</view>
						<view class="keyword" v-if="forbid==''">
							<view v-for="(item,index) in key_list" @tap="doSearch(item.name)" :key="index">
								{{item.name}}
							</view>
						</view>
						<view class="hide-hot-tis" v-else>
							<view>当前搜热门搜索已隐藏</view>
						</view>
					</view>

					<shopList v-if="!isinit" :list="shop_list" :islog="islog" :log="log"> </shopList>
				</scroll-view>
			</view>

		</view>

		<tabbar>

		</tabbar>
	</view>
</template>

<script>
	//引用mSearch组件，如不需要删除即可
	import mSearch from '@/components/mehaotian-search-revision/mehaotian-search-revision.vue';
	import shopList from "./list1.vue";
 
	export default {

		data() {
			return {
				defaultKeyword: "",
				keyword: "",
				oldKeywordList: [],
				hotKeywordList: [],
				keywordList: [],
				forbid: '',
				isShowKeywordList: false,
				shop_list: [],
				tabindex: 0,
				islog: true,
				isinit: true,
				log: '加载中',
				url: '',
				page: 1,
				key_list: []
			}
		},
		onLoad() {

			var pages = getCurrentPages()
			let currentRoute = pages[pages.length - 1].route;
			this.api_path = currentRoute.match(/pages\/(\S*)\//)[1];
			var set_find = this.get_plugin_set

			this.defaultKeyword = set_find.shop_search_placeholder
			console.log("this.defaultKeyword: " + JSON.stringify(this.defaultKeyword));
			if (this.keyword) {
				this.shop_init()
			}
			this.init();

		},
		components: {

			mSearch,
			shopList,
	 
		},
		methods: {
			init() {
			
				this.loadOldKeyword();
				this.loadHotKeyword();

				var that = this;
				let requestParams = {};

				this.Http.post('/plugin.php/banmichaoqiangbizhi/api/index/search_key', requestParams).then(ret => {
					if (ret.status == 200) {
						this.key_list = ret.data.list.data;
					}
				})


			},
			input_del() {
				this.keyword = '';
				this.isinit = true;
				this.islog = true;
			},
			shop_init() {
				var that = this;
				let requestParams = {
					page: this.page,
					keyword: this.keyword,
					tabindex: 0
				};

				this.Http.post('/plugin.php/banmichaoqiangbizhi/api/index/shop_search', requestParams).then(ret => {


					if (ret.status == 200) {
						if (that.page == 1) {
							this.shop_list = ret.data.shop_list.data;
						} else {
							if (ret.data.shop_list.data.length > 0) {
								for (var i = 0; i < ret.data.shop_list.data.length; i++) {
									that.shop_list.push(ret.data.shop_list.data[i]);
								}
							}
							if (ret.data.shop_list.data< 10) {
								this.log = "没有数据了"
							}
						}
						setTimeout(() => {
							that.isinit = false;
							that.islog = false;
						}, 100)


					}



				})
			},
			blur() {
				uni.hideKeyboard()
			},
		 
			//加载历史搜索,自动读取本地Storage
			loadOldKeyword() {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						var OldKeys = JSON.parse(res.data);
						this.oldKeywordList = OldKeys;
					}
				});
			},
			//加载热门搜索
			loadHotKeyword() {
				//定义热门搜索关键字，可以自己实现ajax请求数据再赋值
				this.hotKeywordList = ['键盘', '鼠标', '显示器', '电脑主机', '蓝牙音箱', '笔记本电脑', '鼠标垫', 'USB', 'USB3.0'];
			},
			//监听输入
			inputChange(event) {
				//兼容引入组件时传入参数情况
				var keyword = event.detail ? event.detail.value : event;

				// if (!keyword) {
				// 	this.keywordList = [];
				// 	this.isShowKeywordList = false;
				// 	return;
				// }
				// this.isShowKeywordList = true;

			},
			//高亮关键字
			drawCorrelativeKeyword(keywords, keyword) {
				var len = keywords.length,
					keywordArr = [];
				for (var i = 0; i < len; i++) {
					var row = keywords[i];
					//定义高亮#9f9f9f
					var html = row[0].replace(keyword, "<span style='color: #9f9f9f;'>" + keyword + "</span>");
					html = '<div>' + html + '</div>';
					var tmpObj = {
						keyword: row[0],
						htmlStr: html
					};
					keywordArr.push(tmpObj)
				}
				return keywordArr;
			},
			//顶置关键字
			setKeyword(index) {
				this.keyword = this.keywordList[index].keyword;
			},
			//清除历史搜索
			oldDelete() {
				uni.showModal({
					content: '确定清除历史搜索记录？',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							this.oldKeywordList = [];
							uni.removeStorage({
								key: 'OldKeys'
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//热门搜索开关
			hotToggle() {
				this.forbid = this.forbid ? '' : '_forbid';
			},
			//执行搜索
			doSearch(keyword) {
				keyword = keyword === false ? this.keyword : keyword;
				this.keyword = keyword;
				this.saveKeyword(keyword); //保存为历史 
				this.shop_init();

			},
			//保存关键字到历史记录
			saveKeyword(keyword) {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						var OldKeys = JSON.parse(res.data);
						var findIndex = OldKeys.indexOf(keyword);
						if (findIndex == -1) {
							OldKeys.unshift(keyword);
						} else {
							OldKeys.splice(findIndex, 1);
							OldKeys.unshift(keyword);
						}
						//最多10个纪录
						OldKeys.length > 10 && OldKeys.pop();
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					},
					fail: (e) => {
						var OldKeys = [keyword];
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					}
				});
			}
		}
	}
</script>
<style>
	.search-box {
		width: 100%;
		background-color: rgb(242, 242, 242);
		padding: 15upx 2.5%;
		display: flex;
		justify-content: space-between;
		position: sticky;
		top: 0;
	}

	.search-box .mSearch-input-box {
		width: 100%;
	}

	.search-box .input-box {
		width: 85%;
		flex-shrink: 1;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.search-box .search-btn {
		width: 15%;
		margin: 0 0 0 2%;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-shrink: 0;
		font-size: 28upx;
		color: #fff;
		background: linear-gradient(to right, #ff9801, #ff570a);
		border-radius: 60upx;
	}

	.search-box .input-box>input {
		width: 100%;
		height: 60upx;
		font-size: 32upx;
		border: 0;
		border-radius: 60upx;
		-webkit-appearance: none;
		-moz-appearance: none;
		appearance: none;
		padding: 0 3%;
		margin: 0;
		background-color: #ffffff;
	}

	.placeholder-class {
		color: #9e9e9e;
	}

	.search-keyword {
		width: 100%;
		background-color: rgb(242, 242, 242);
	}

	.keyword-list-box {
		height: calc(100vh - 110upx);
		padding-top: 10upx;
		border-radius: 20upx 20upx 0 0;
		background-color: #fff;
	}

	.keyword-entry-tap {
		background-color: #eee;
	}

	.keyword-entry {
		width: 94%;
		height: 80upx;
		margin: 0 3%;
		font-size: 30upx;
		color: #333;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: solid 1upx #e7e7e7;
	}

	.keyword-entry image {
		width: 60upx;
		height: 60upx;
	}

	.keyword-entry .keyword-text,
	.keyword-entry .keyword-img {
		height: 80upx;
		display: flex;
		align-items: center;
	}

	.keyword-entry .keyword-text {
		width: 90%;
	}

	.keyword-entry .keyword-img {
		width: 10%;
		justify-content: center;
	}

	.keyword-box {
		height: calc(100vh - 110upx);
		border-radius: 20upx 20upx 0 0;
		background-color: #fff;
	}

	.keyword-box .keyword-block {
		padding: 10upx 0;
	}

	.keyword-box .keyword-block .keyword-list-header {
		width: 94%;
		padding: 10upx 3%;
		font-size: 27upx;
		color: #333;
		display: flex;
		justify-content: space-between;
	}

	.keyword-box .keyword-block .keyword-list-header image {
		width: 40upx;
		height: 40upx;
	}

	.keyword-box .keyword-block .keyword {
		width: 94%;
		padding: 3px 3%;
		display: flex;
		flex-flow: wrap;
		justify-content: flex-start;
	}

	.keyword-box .keyword-block .hide-hot-tis {
		display: flex;
		justify-content: center;
		font-size: 28upx;
		color: #6b6b6b;
	}

	.keyword-box .keyword-block .keyword>view {
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 60upx;
		padding: 0 20upx;
		margin: 10upx 20upx 10upx 0;
		height: 60upx;
		font-size: 28upx;
		background-color: rgb(242, 242, 242);
		color: #6b6b6b;
	}
</style>